package com.example.attendancesystem.Service.Imp;

import com.example.attendancesystem.Mapper.*;
import com.example.attendancesystem.Models.*;
import com.example.attendancesystem.Service.EmpService;
import com.example.attendancesystem.Tools.CheckTimeTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class EmpServiceImp implements EmpService {

    @Autowired
    private CheckTimeMapper checkTimeMapper;
    @Autowired
    private MorRowMapper morRowMapper;
    @Autowired
    private AftRowMapper aftRowMapper;
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private LeaveRowMapper leaveRowMapper;
    @Autowired
    private EmployeesMapper employeesMapper;

    @Override
    public CheckTime getCheckTime() {
        CheckTime checkTime;
        checkTime = checkTimeMapper.getCheckTime();
        return checkTime;
    }

    @Override
    public MorRow getMorRowByEmpNumberAndDate(String empNumber, String queryDate) {
        MorRow morRow;
        morRow = morRowMapper.getMorRowByEmpNumAndDate(empNumber, queryDate);
        return morRow;
    }

    @Override
    public AftRow getAftRowByEmpNumberAndDate(String empNumber, String queryDate) {
        return aftRowMapper.getAftRowByEmpNumAndDate(empNumber, queryDate);
    }

    /**
     * 打卡
     *
     * @param empNumber 员工号
     * @return 1001 正常打卡成功， 1002 迟到打卡 ， 1003 早退打卡， 1004 打卡已经打卡了 ，1005 不在打卡时间内 ， 1006 打卡失败
     */
    @Override
    public Integer clickClockBtn(String empNumber, String empName) {
        int code;
        // 查询所有打卡时间段 判断处于哪个打卡时间段  或者不处于打卡时间段
        // 获取当前时间与打卡时间的差值 看看是属于正常打卡还是 迟到 早退
        // 向数据库的打卡记录表更新一条记录   更新之前需要先查对应的字段是否有纪录 有就代表已经打卡 没有就插入

        //获取当前时间 格式 yyyy-MM-dd HH:mm:ss
        SimpleDateFormat date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = date1.format(new Date());

        //获取当前日期yyyyMMdd
        SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
        String nowDate = date.format(new Date());
        //查询现在的打卡时间
        CheckTime checkTime = checkTimeMapper.getCheckTime();
        //比较当前时间与打卡时间的差值 看看处于哪个打卡时间段内
        int morOnDiffVal = CheckTimeTools.getTimeDifference(checkTime.getMorOnTime()); // 当前时间减去 打卡时间
        int morOffDiffVal = CheckTimeTools.getTimeDifference(checkTime.getMorOffTime()); // 当前时间减去 打卡时间
        int aftOnDiffVal = CheckTimeTools.getTimeDifference(checkTime.getAftOnTime()); // 当前时间减去 打卡时间
        int aftOffDiffVal = CheckTimeTools.getTimeDifference(checkTime.getAftOffTime()); // 当前时间减去 打卡时间

        // 判断是否是早上上班打卡  -30 <= morOnDiffVal <=30
        if (Math.abs(morOnDiffVal) <= 30) {
            //判断是否是否已经打卡
            MorRow morRow = morRowMapper.getMorRowByEmpNumAndDate(empNumber, nowDate);
            if ((null != morRow) && (null != morRow.getMorOn())) { //早上上班字段已经有记录了 即已经打卡
                code = 1004; // 已经打卡
            } else { //早上上班字段没有记录 即该时间段没有打卡
                int res;
                int onState;  //打卡
                if (morOnDiffVal <= 0) onState = 1; // 判断是正常打卡还是 迟到
                else onState = 2;
                //判断是要执行插入 还是 更新 比如上午上班没有打卡，下班打卡就要执行插入 ； 上午上班有打卡，下班打卡就要执行更新 ；
                // 由于请假 会提前插入 记录 早上上班打卡也要判断
                if (null == morRow) { // 没有记录 执行插入操作
                    res = morRowMapper.insertMorOnRow(empNumber, empName, nowDate, nowTime, onState);
                } else {
                    res = morRowMapper.updateMorOnRow(empNumber, nowDate, nowTime, onState);
                }
                if (res == 1 && onState == 1) code = 1001;  // 上午正常打卡
                else code = 1002; //迟到打卡
            }
        }

        // 判断是否是早上下班打卡  -30 <= morOnDiffVal <=30
        else if (Math.abs(morOffDiffVal) <= 30) {
            //判断是否是否已经打卡
            MorRow morRow = morRowMapper.getMorRowByEmpNumAndDate(empNumber, nowDate);
            if ((null != morRow) && (null != morRow.getMorOff())) { //早上下班字段已经有记录了 即已经打卡
                code = 1004; // 已经打卡
            } else {
                //没有打卡
                //判断是要执行插入 还是 更新 比如上午上班没有打卡，下班打卡就要执行插入 ； 上午上班有打卡，下班打卡就要执行更新 ；
                int res; //打卡结果影响数据条数
                int offState;
                if (morOffDiffVal <= 0) offState = 2; // 判断是正常打卡还是 迟到
                else offState = 1;
                if (null == morRow) { // 没有记录 执行插入操作
                    res = morRowMapper.insertMorOffRow(empNumber, empName, nowDate, nowTime, offState);
                } else {
                    res = morRowMapper.updateMorOffRow(empNumber, nowDate, nowTime, offState);
                }
                if (res == 1 && offState == 1) code = 1001; //正常打卡
                else code = 1003; // 早退打卡
            }
        }

        //判断是否属于下午上班时间段内
        else if (Math.abs(aftOnDiffVal) <= 30) {
            //判断是否是否已经打卡
            AftRow aftRow = aftRowMapper.getAftRowByEmpNumAndDate(empNumber, nowDate);
            if ((null != aftRow) && (null != aftRow.getAftOn())) { //下午上班字段已经有记录了 即已经打卡
                code = 1004; // 已经打卡
            } else { //下午上班字段没有记录 即该时间段没有打卡
                int onState;
                if (aftOnDiffVal <= 0) onState = 1; // 判断是正常打卡还是 迟到
                else onState = 2;
                int res;
                if (aftRow == null) { // 没有记录 执行插入操作
                    res = aftRowMapper.insertAftOnRow(empNumber, empName, nowDate, nowTime, onState);
                } else {
                    res = aftRowMapper.updateAftOnRow(empNumber, nowDate, nowTime, onState);
                }
                if (res == 1 && onState == 1) code = 1001;
                else code = 1002;
            }
        }

        //判断是否下午下班时间段
        else if (Math.abs(aftOffDiffVal) <= 30) {
            AftRow aftRow = aftRowMapper.getAftRowByEmpNumAndDate(empNumber, nowDate);
            if ((null != aftRow) && (null != aftRow.getAftOff())) { //下午下班字段已经有记录了 即已经打卡
                code = 1004; // 已经打卡
            } else { //下午上班字段没有记录 即该时间段没有打卡
                int offState;
                if (aftOffDiffVal <= 0) offState = 2; // 判断是正常打卡还是 早退
                else offState = 1;
                int res;
                if (null == aftRow) { // 没有记录 执行插入操作
                    res = aftRowMapper.insertAftOffRow(empNumber, empName, nowDate, nowTime, offState);
                } else {
                    res = aftRowMapper.updateAftOffRow(empNumber, nowDate, nowTime, offState);
                }
                if (res == 1 && offState == 1) code = 1001;
                else code = 1003;
            }
        } else code = 1005;
        return code;
    }

    @Override
    public List<News> getNewsLimit() {
        List<News> news;
        news = newsMapper.getNewsLimit();
        return news;
    }

    @Override
    public Boolean addEmpLeaveRequest(LeaveRow leaveRow) {
        //先查询是否已经有了记录
        int sum = leaveRowMapper.getLeaveRowByDateAndEmpNumberAndTime(leaveRow.getEmpNumber(), leaveRow.getLeaveDate());
        if (sum != 0) return false;
        //根据请假时间和日期 向打卡表中插入一条记录
        switch (leaveRow.getLeaveTime()) {
            case "上午":
                morRowMapper.insertMorOnRow(
                        leaveRow.getEmpNumber(),
                        leaveRow.getEmpName(), leaveRow.getLeaveDate(),
                        "------------", 3);
                morRowMapper.updateMorOffRow(leaveRow.getEmpNumber(), leaveRow.getLeaveDate(),
                        "------------", 3);
                break;
            case "下午":
                aftRowMapper.insertAftOnRow(leaveRow.getEmpNumber(),
                        leaveRow.getEmpName(), leaveRow.getLeaveDate(),
                        "------------", 3);
                aftRowMapper.updateAftOffRow(leaveRow.getEmpNumber(), leaveRow.getLeaveDate(),
                        "------------", 3);
                break;
            default:
                break;
        }
        int res = leaveRowMapper.addEmpLeaveRequest(leaveRow);
        return res == 1;
    }

    @Override
    public Boolean updateEmpImg(String empNumber, String imgPath) {
        int res = employeesMapper.updateEmpImgPath(empNumber,imgPath);
        return res == 1;
    }

    @Override
    public DataMorRow getEmpMorRow(String empNumber, String queryDate) {
        queryDate += "%";
        DataMorRow dataMorRow = new DataMorRow();
        dataMorRow.setCode(0);
        dataMorRow.setMsg("");
        List<MorRow> morRows = morRowMapper.getEmpMorRowByDate( empNumber, queryDate );
        dataMorRow.setData(morRows);
        dataMorRow.setCount(morRows.size());
        return dataMorRow;
    }

    @Override
    public DataAftRow getEmpAftRow(String empNumber, String queryDate) {
        queryDate += "%";
        DataAftRow dataAftRow = new DataAftRow();
        dataAftRow.setCode(0);
        dataAftRow.setMsg("");
        List<AftRow> aftRows = aftRowMapper.getEmpAftRowByDate( empNumber,  queryDate );
        dataAftRow.setData(aftRows);
        dataAftRow.setCount(aftRows.size());
        return dataAftRow;
    }

    @Override
    public DataLeaveRow getEmpLeaveRow(String empNumber, String queryDate) {
        queryDate += "%";
        DataLeaveRow dataLeaveRow = new DataLeaveRow();
        dataLeaveRow.setCode(0);
        dataLeaveRow.setMsg("");
        List<LeaveRow> leaveRows = leaveRowMapper.getEmpLeaveRowByDate( empNumber,   queryDate );
        dataLeaveRow.setData(leaveRows);
        dataLeaveRow.setCount(leaveRows.size());
        return dataLeaveRow;
    }

    @Override
    public DataNews getAllNews(String title) {
        DataNews dataNews = new DataNews();
        dataNews.setCode(0);
        dataNews.setMsg("");
        List<News> News = newsMapper.getAllNews(title);
        dataNews.setData(News);
        dataNews.setCount(News.size());
        return dataNews;
    }



}
